const tree = require('./tree')

// 深度优先遍历，先输出当前节点，然后遍历子节点，对子节点进行深度遍历
const dfs = (root) => {
    console.log(root.val)
    root.children.forEach(item => {
        dfs(item)
    })
}

// 使用栈实现
const dfs1 = (root) => {

    const res = []
    const stack = [root]
    while(stack.length) {
        const node = stack.pop()
        res.push(node.val)
        for (let i = node.children.length - 1; i >= 0; i--) {  // 倒叙将子节点放入栈中
            stack.push(node.children[i])
        }
    }
    console.log(res)
}

dfs(tree)
dfs1(tree)